package com.qipay.sp.excutor.yitong;

import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiStatusEnum;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.JsonUtils;
import com.qipay.utils.SignSortMD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class YtWxTradeQueryExcutor extends AbstractExcutor {

    private final static String API_URL = "http://120.77.22.14:8380/pay/gateway/query.do";

    private final static String SIGN_NAME = "SIGN_DAT";
    private final static String KEY_NAME = "KEY";
    private final static String CONFIG_SECRET = "key";
    private final static String CONFIG_MCHID = "mch_id";
    private final static String CONFIG_PRT_CODE = "prt_code";

    private final static String API_PARAM_PRT_CODE="PRT_CODE";
    private final static String API_PARAM_VER_NO="VER_NO";
    private final static String API_PARAM_MERC_ID="MERC_ID";
    private final static String API_PARAM_ORDER_NO="ORDER_NO";
    private final static String API_PARAM_NON_STR="NON_STR";
    private final static String API_PARAM_TM_SMP="TM_SMP";
    private final static String API_PARAM_SIGN_TYP="SIGN_TYP";
    private final static String API_PARAM_SIGN_DAT="SIGN_DAT";

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.WX_TRADE_QUERY;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put(API_PARAM_PRT_CODE, getVal(spconfig, CONFIG_PRT_CODE));
        apiParams.put(API_PARAM_VER_NO, "1.0");
        apiParams.put(API_PARAM_MERC_ID, getVal(spconfig, CONFIG_MCHID));
        apiParams.put(API_PARAM_ORDER_NO, order.getTradeNo());
        apiParams.put(API_PARAM_NON_STR, new Date().getTime());
        apiParams.put(API_PARAM_TM_SMP, System.currentTimeMillis());
        apiParams.put(API_PARAM_SIGN_TYP, "MD5");

        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        Object secret = getVal(spconfig, CONFIG_SECRET);

        String sign = SignSortMD5Utils.getSign(params,secret.toString(),SIGN_NAME,KEY_NAME,false);

        params.put(API_PARAM_SIGN_DAT, sign);

        String result = sendPost(JsonUtils.toJson(params), API_URL);
        if (StringUtils.isBlank(result)) {
            throw new Exception("请求娱秀互动的微信WAP查询接口失败");
        }

        Map<String, Object> responseMap = JsonUtils.toMap(result);
        Map<String, Object> resultMap = new HashMap<>();

        if (responseMap == null || !responseMap.containsKey("RETURNCODE") ) {
            logger.error("请求娱秀互动的微信WAP查询接口失败:" + result);
            return null;
        }

        if(!responseMap.get("RETURNCODE").toString().equals("0000"))
        {
            logger.error("请求娱秀互动的微信WAP查询接口业务执行失败:" + result);
            resultMap.put("result_code", "1");
            resultMap.put("err_code", ApiStatusEnum.SP_ERR.getCode());
            resultMap.put("err_code_des", responseMap.get("RETURNCON"));
            return resultMap;
        }

        resultMap.put("result_code", "0");

        resultMap.put("trade_state", "1".equals(responseMap.get("ORD_STS"))? "SUCCESS":"NOTPAY");
        resultMap.put("trade_no", responseMap.get("ORDER_NO"));
        resultMap.put("out_transaction_id", responseMap.get("OUT_TRADE_NO"));

        return resultMap;
    }
}
